package com.example.myanimator.shadowLayer;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.example.myanimator.R;


/**
 * Created by chenxuan on 2020/7/15.
 *
 * 自定义带阴影的textView
 */
public class ShadowTextView extends AppCompatTextView {
    private int shadowColor;

    public ShadowTextView(Context context) {
        this(context, null);
    }

    public ShadowTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShadowTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {

        if (null != attrs) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ShadowTextView);
            shadowColor = typedArray.getColor(R.styleable.ShadowTextView_spaceShadowColor, Color.GREEN);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        getPaint().setShadowLayer(15, 0.5f, 0.5f, shadowColor);
        super.onDraw(canvas);
    }

    public void setShadowColor(int color) {
        shadowColor = color;
        postInvalidate();
    }
}
